// 监听扩展安装事件
chrome.runtime.onInstalled.addListener(() => {
  console.log('抖音大人后台视频爬取工具已安装');
  
  // 初始化存储
  chrome.storage.local.set({
    isRunning: false,
    currentPage: 0,
    totalPages: 0,
    videoCount: 0,
    pageCount: 10,
    scrollDelay: 1500,
    scrapedData: []
  });
});

// 监听网络请求
chrome.webRequest.onBeforeRequest.addListener(
  function(details) {
    // 检查是否是目标API
    if (details.url.includes('/api/anchor/creative/search_videos')) {
      // 发送消息到content script
      chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        if (tabs.length > 0) {
          chrome.tabs.sendMessage(tabs[0].id, {
            action: 'apiDetected',
            url: details.url
          });
        }
      });
    }
    return {cancel: false};
  },
  {urls: ["https://buyin.jinritemai.com/api/*"]},
  ["requestBody"]
);

// 监听来自content script的消息
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if (request.action === 'updateProgress') {
    // 更新进度
    chrome.storage.local.set({
      currentPage: request.currentPage,
      videoCount: request.videoCount
    });
    sendResponse({status: 'updated'});
  }
  
  return true;
}); 